{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "84240757-e849-4128-9265-e597d453837f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from enum import Enum\n",
    "\n",
    "Month = Enum('Month', ('Jan', 'Feb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2d0da160-01b3-41c1-a813-b8d4fead15b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jan >= Month.Jan , 1\n",
      "Feb >= Month.Feb , 2\n"
     ]
    }
   ],
   "source": [
    "for name, member in Month.__members__.items():\n",
    "    print(name, '>=', member, ',', member.value) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e5520adb-7d0d-402d-bee0-29997e258667",
   "metadata": {},
   "outputs": [],
   "source": [
    "from enum import unique, Enum\n",
    "\n",
    "@unique\n",
    "class Weekday(Enum):\n",
    "    Sun = 0 # Sun的value被设定为0\n",
    "    Mon = 1\n",
    "    Tue = 2\n",
    "    Wed = 3\n",
    "    Thu = 4\n",
    "    Fri = 5\n",
    "    Sat = 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "939da84a-f3e0-4475-8fe1-8eb555e1b58b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Weekday.Mon\n"
     ]
    }
   ],
   "source": [
    "day1 = Weekday.Mon\n",
    "print(day1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a8f83a9f-a154-4482-85a5-ad1559b4f08c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Weekday.Tue\n"
     ]
    }
   ],
   "source": [
    "\n",
    "print(Weekday.Tue)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ba131d58-2bfc-4852-9668-dfbea9360192",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "print(Weekday.Tue.value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "bf4db6d4-3abb-4c5e-9350-13e13e34fa81",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Weekday.Fri\n"
     ]
    }
   ],
   "source": [
    "print(Weekday(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a9839a6f-5053-4e32-b3f3-cb2ba747dc81",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "7 is not a valid Weekday",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[16], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mWeekday\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m7\u001b[39;49m\u001b[43m)\u001b[49m)\n",
      "File \u001b[1;32m~\\.conda\\envs\\chatgpt\\lib\\enum.py:384\u001b[0m, in \u001b[0;36mEnumMeta.__call__\u001b[1;34m(cls, value, names, module, qualname, type, start)\u001b[0m\n\u001b[0;32m    359\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    360\u001b[0m \u001b[38;5;124;03mEither returns an existing member, or creates a new enum class.\u001b[39;00m\n\u001b[0;32m    361\u001b[0m \n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    381\u001b[0m \u001b[38;5;124;03m`type`, if set, will be mixed in as the first base class.\u001b[39;00m\n\u001b[0;32m    382\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    383\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m names \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:  \u001b[38;5;66;03m# simple value lookup\u001b[39;00m\n\u001b[1;32m--> 384\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mcls\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__new__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mcls\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    385\u001b[0m \u001b[38;5;66;03m# otherwise, functional API: we're creating a new Enum type\u001b[39;00m\n\u001b[0;32m    386\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m_create_(\n\u001b[0;32m    387\u001b[0m         value,\n\u001b[0;32m    388\u001b[0m         names,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    392\u001b[0m         start\u001b[38;5;241m=\u001b[39mstart,\n\u001b[0;32m    393\u001b[0m         )\n",
      "File \u001b[1;32m~\\.conda\\envs\\chatgpt\\lib\\enum.py:702\u001b[0m, in \u001b[0;36mEnum.__new__\u001b[1;34m(cls, value)\u001b[0m\n\u001b[0;32m    700\u001b[0m ve_exc \u001b[38;5;241m=\u001b[39m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m%r\u001b[39;00m\u001b[38;5;124m is not a valid \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m%\u001b[39m (value, \u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__qualname__\u001b[39m))\n\u001b[0;32m    701\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m result \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m exc \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m--> 702\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m ve_exc\n\u001b[0;32m    703\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m exc \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m    704\u001b[0m     exc \u001b[38;5;241m=\u001b[39m \u001b[38;5;167;01mTypeError\u001b[39;00m(\n\u001b[0;32m    705\u001b[0m             \u001b[38;5;124m'\u001b[39m\u001b[38;5;124merror in \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m._missing_: returned \u001b[39m\u001b[38;5;132;01m%r\u001b[39;00m\u001b[38;5;124m instead of None or a valid member\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m    706\u001b[0m             \u001b[38;5;241m%\u001b[39m (\u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m, result)\n\u001b[0;32m    707\u001b[0m             )\n",
      "\u001b[1;31mValueError\u001b[0m: 7 is not a valid Weekday"
     ]
    }
   ],
   "source": [
    "print(Weekday(7))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f735c3de-4a72-44f3-93a5-06628748f86e",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Gender(Enum):\n",
    "    Male = 0\n",
    "    Female = 1\n",
    "\n",
    "class Student(object):\n",
    "    def __init__(self, name, gender):\n",
    "        self.name = name\n",
    "        self.gender = gender"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "418afd8d-9014-4d95-a884-100a2c56e39a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试通过!\n"
     ]
    }
   ],
   "source": [
    "bart = Student('Bart', Gender.Male)\n",
    "if bart.gender == Gender.Male:\n",
    "    print('测试通过!')\n",
    "else:\n",
    "    print('测试失败!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a434f15c-ce01-4848-981e-5c4cc4294ab6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
